$(function() {
	layui.use([ 'form', 'table', 'laypage', 'layer', 'laydate' ], function(form, table, laypage, layer, laydate) {
		
		var params = {};
		//广告位预留
		/*layer.open({
			type: 1,
			title: false,
			closeBtn: 0,
			shadeClose: true,
			skin: '',
			content: ''
		});*/
		
		var loadData = function(page) {
			function genMenu(data) {

				var html = '';
				$.each(data,function (index,value){
					var permenu = value.data;
					html += '<li>';
					html += '<a href="javascript:;">\n' ;
					html += '<i class="iconfont left-nav-li" lay-tips="'+permenu.name+'">'+permenu.pageIcon+'</i>\n' ;
					html += '<cite>'+permenu.name+'</cite>\n' ;
					html += '<i class="iconfont nav_right">'+'&#xe697;'+'</i></a>';
					html += '<ul class="sub-menu">';
					$.each(value.children,function (index,res) {
						var subs = res.data;

						html += '<li>\n';
						html += '<a onclick="xadmin.add_tab(\''+subs.name+'\',\''+subs.url+'\')">\n';
						html += '<i class="iconfont">'+subs.pageIcon+'</i>\n';
						html += '<cite>'+subs.name+'</cite></a>\n';
						html += '</li>';

					});
					html += '</ul>';
					html += '</li>';
				});

				return html;
			}

			CoreUtil.sendAjax("/permission/autoGenMenu", JSON.stringify(params), function(res) {
					if('0' == res.code){
						$(".khzsSubjectNav").html(genMenu(res.data.children));
					}else{
						layer.msg(res.msg);
					}
	              
			}, "POST", false);
			
		};
		
		
		$("#logOut").click(function() {
			CoreUtil.sendAjax("/sec/operator/logout", null, function(res) {
				if('0' == res.code){
					layer.msg(JSON.stringify(res.msg),function(){
		                  location.href=res.data.toPage
		              });
				}else{
					layer.msg(res.msg);
				}
			}, "POST", false, null);
		});

		var initUserInfo = function(){
			var userInfo = window.sessionStorage.getItem("user");

			var parseJSON = $.parseJSON(userInfo);

			$('#userInfo').html("欢迎"+parseJSON.code+"-"+parseJSON.opetatorName);
		};

		$('#loginOut').on('click',function (){
			$.post("/operator/loginOut","",function (data) {
				if (data.code == '0'){
					parent.location.href = "./login.html"
				}
			})
		});



		initUserInfo();
		loadData();
		
	});
});